<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Wilead\ApiBundle\Controller;

use FOS\RestBundle\Controller\Annotations\RequestParam;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use FOS\RestBundle\View\RouteRedirectView;

/**
 * Description of UserAttachmentController
 *
 * @author jack
 */
class UserAttachmentController extends ApiController {

    const ALBUMS = 1;
    const VIDEO = 2;

    /**
     * @ApiDoc(
     *      description="添加用户图片"
     * )
     * @RequestParam(name="User", requirements="integer", description="用户ID")
     * @RequestParam(name="thumbnailsUrl", requirements="string", description="相片缩略图地址")
     * @RequestParam(name="url", requirements="string", description="相片地址")
     * @RequestParam(name="description", requirements="string", description="相片描述（非必填）")
     */
    public function albumsAddAction() {
        return $this->add(self::ALBUMS);
    }

    /**
     * @ApiDoc(
     *      description="删除用户图片"
     * )
     * @RequestParam(name="id", requirements="integer", description="图片ID")
     */
    public function albumsDeleteAction() {
        return $this->delete(self::ALBUMS);
    }

    /**
     * @ApiDoc(
     *      description="添加用户视频"
     * )
     * @RequestParam(name="User", requirements="integer", description="用户ID")
     * @RequestParam(name="thumbnailsUrl", requirements="string", description="视频缩略图地址")
     * @RequestParam(name="url", requirements="string", description="视频地址")
     * @RequestParam(name="description", requirements="string", description="视频描述（非必填）")
     */
    public function videoAddAction() {
        return $this->add(self::VIDEO);
    }

    /**
     * @ApiDoc(
     *      description="删除用户视频"
     * )
     * @RequestParam(name="id", requirements="integer", description="视频ID")
     */
    public function videoDeleteAction() {
        return $this->delete(self::VIDEO);
    }

    private function add($type) {
        switch ($type) {
            case self::ALBUMS:
                $entity = new \Wilead\IArtistBundle\Entity\UserAlbums;
                $formType = new \Wilead\IArtistBundle\Form\UserAlbumsType;
                break;
                ;
            case self::VIDEO:
                $entity = new \Wilead\IArtistBundle\Entity\UserVideo;
                $formType = new \Wilead\IArtistBundle\Form\UserVideoType;
                break;
        }
        $form = $this->createForm($formType, $entity);
        $form->bind($this->getRequest());
        $errors = $this->validatorEntity($entity);
        if (count($errors) == 0) {
            $em = $this->getEntityManager();
            $em->persist($entity);
            $em->flush();
            return RouteRedirectView::create('wilead_api_user_get', array('id' => $entity->getUser()->getId()), 200);
        } else {
            return $this->getErrorsView($errors);
        }
    }

    private function delete($type) {
        $request = $this->getRequest();
        if ($request->request->has('id')) {
            switch ($type) {
                case self::ALBUMS:
                    $repository = 'WileadIArtistBundle:UserAlbums';
                    break;
                case self::VIDEO:
                    $repository = 'WileadIArtistBundle:UserVideo';
                    break;
            }
            $entity = $this->getEntityManager()->getRepository($repository)->find($request->request->get('id'));

            if (!$entity)
                throw $this->createNotFoundException();

            $userId = $entity->getUser()->getId();
            $em = $this->getEntityManager();
            $em->remove($entity);
            $em->flush();

            return RouteRedirectView::create('wilead_api_user_get', array('id' => $userId), 200);
        } else {
            throw $this->createNotFoundException();
        }
    }

}

?>
